package fox.framework.rpc.server.dispatcher.invoke;

import fox.framework.rpc.server.message.request.RpcServerRequest;
import fox.framework.rpc.server.message.response.RpcServerResponse;


import java.util.List;

/**
 * Rpc服务端方法调用链
 * @author cuichao
 * @Description: ProvideInvokerChain
 * @Date: create in 2021/1/12 20:41
 */
public abstract class ProvideInvokerChain {


    private List<ProvideInvoker> chains;
    private int currentIndex = -1;


    public ProvideInvokerChain(List<ProvideInvoker> chains) {
        this.chains = chains;
    }

    /**
     * 执行调用链
     * @param request
     * @return
     * @throws Throwable
     */
    public void doChain(RpcServerRequest request, RpcServerResponse response) throws Throwable {
        if ( currentIndex == chains.size() - 1) {
             invoke(request,response);
        } else {
             chains.get(++currentIndex).doInvoke(this, request,response);
        }
    }


    /**
     * 过滤器链最终执行方法
     *
     * @param request
     * @return
     * @throws Throwable
     */
    protected abstract void invoke(RpcServerRequest request, RpcServerResponse response) throws Throwable;




}
